package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.service.OrderSettingService;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> list) {
        //遍历集合
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否进行了预约设置
                long countByOrderData = orderSettingDao.findCountByOrdeDate(orderSetting.getOrderDate());
                if (countByOrderData > 0) {
                    //有了预约设置 修改
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //没有人预约 进行添加
                    orderSettingDao.add(orderSetting);
                }
            }
        }


    }

    //根据月份查询 对应的预约人数
    public List<Map> getOrderSettingByMonth(String date) {
        String begin=date+"-1";
        String end=date+"-31";
        Map<String,String> map=new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        //调用dao  根据日期范围查询 对应的预约设置
       List<OrderSetting> list= orderSettingDao.getOrderSettingByMonth(map);
       List<Map> list1=new ArrayList<>();
       if(list!=null&&list.size()>0){
           for (OrderSetting orderSetting : list) {
               Map<String ,Object> m=new HashMap<>();
               m.put("date",orderSetting.getOrderDate().getDate());//获取日期 几号
               m.put("number",orderSetting.getNumber());
               m.put("reservations",orderSetting.getReservations());
               list1.add(m);
           }
       }
        return list1;
    }

        // 根据数据对 预约设置进行操作
    public void editNumberByDate(OrderSetting orderSetting) {
        //获取当前的日期
        Date orderDate = orderSetting.getOrderDate();
        //查询当前时间是否进行了 预约设置
        long count = orderSettingDao.findCountByOrdeDate(orderDate);
        //判断是否有值
        if(count>0){
            //有值 进行修改
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //每页值进行添加
            orderSettingDao.add(orderSetting);
        }

    }
}
